﻿using CuocChienQueNha.Class.Basic;
using CuocChienQueNha.Class.File;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System;
using System.Windows;
using System.Windows.Navigation;

namespace CuocChienQueNha
{
    public partial class MainPage : PhoneApplicationPage
    {
        private ContentManager contentManager;
        private GameTimer timer;
        private SpriteBatch spriteBatch;

        private Texture2D bg, amination_skill, text_continue, logo;
        private int countAmination;
        private ButtonGame btn_setting, btn_exit, btn_rate, btn_share, btn_info, btn_help;

        private Vector2 start, curtouch;

        private string giaodien;

        //tai nguyen setting
        private AudioSetting audiosetting;
        private bool isMusic, isSound;
        private Texture2D bgsetting, img_music, img_sound;
        private ButtonGame btn_ok;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;
            giaodien = "menu";
            audiosetting = new AudioSetting();
            isMusic = audiosetting.isMusic;
            isSound = audiosetting.isSound;
            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            btn_exit = new ButtonGame("exit", "Images/Menu/exit", new Vector2(730, 15), new Vector2(56, 28), 1.714f);
            btn_setting = new ButtonGame("setting", "Images/Menu/button-setting", new Vector2(670, 15), new Vector2(68, 34), 1.411f);
            btn_rate = new ButtonGame("rate", "Images/Menu/button-rate", new Vector2(610, 15), new Vector2(96, 48), 1f);
            btn_share = new ButtonGame("share", "Images/Menu/button-shareF", new Vector2(550, 15), new Vector2(96, 48), 1f);
            btn_info = new ButtonGame("info", "Images/Menu/button-info", new Vector2(490, 15), new Vector2(96, 48), 1f);
            btn_help = new ButtonGame("help", "Images/Menu/button-help", new Vector2(430, 15), new Vector2(96, 48), 1f);
            btn_ok = new ButtonGame("ok", "Images/Setting/Button-Ok", new Vector2(342, 310), new Vector2(232, 48), 1f);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here
            bg = contentManager.Load<Texture2D>("Images/Menu/background1");
            logo = contentManager.Load<Texture2D>("Images/Menu/Logo");
            text_continue = contentManager.Load<Texture2D>("Images/Menu/Touch2");
            countAmination = 0;
            btn_exit.Load(contentManager);
            btn_setting.Load(contentManager);
            btn_info.Load(contentManager);
            btn_rate.Load(contentManager);
            btn_share.Load(contentManager);
            btn_help.Load(contentManager);

            bgsetting = contentManager.Load<Texture2D>("Images/Setting/setting");
            img_music = contentManager.Load<Texture2D>("Images/Setting/" + (isMusic ? "v" : "x"));
            img_sound = contentManager.Load<Texture2D>("Images/Setting/" + (isSound ? "v" : "x"));
            btn_ok.Load(contentManager);
            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            base.OnNavigatedFrom(e);
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (giaodien.Equals("menu"))
            {
                MessageBoxResult m = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "", MessageBoxButton.OKCancel);
                if (m == MessageBoxResult.OK)
                {
                    // Application.Current.Terminate();
                    Microsoft.Xna.Framework.Game game = new Microsoft.Xna.Framework.Game();
                    game.Exit();
                }
            }
            else if (giaodien.Equals("setting"))
            {
                audiosetting.SaveSetting(isMusic, isSound);
                giaodien = "menu";
            }
            e.Cancel = true;
        }
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here

            TouchCollection touchs = TouchPanel.GetState();
            foreach (TouchLocation touch in touchs)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    start = touch.Position;
                    curtouch = touch.Position;
                }
                if (touch.State == TouchLocationState.Moved)
                {
                    curtouch = touch.Position;
                    #region
                    if (giaodien.Equals("menu"))
                    {
                        btn_setting.isTouch(curtouch);
                        btn_exit.isTouch(curtouch);
                        btn_rate.isTouch(curtouch);
                        btn_share.isTouch(curtouch);
                        btn_info.isTouch(curtouch);
                        btn_help.isTouch(curtouch);
                    }
                    else
                    {
                        btn_ok.isTouch(curtouch);
                    }
                    #endregion
                }
                if (touch.State == TouchLocationState.Released)
                {
                    curtouch = touch.Position;
                    #region
                    if (Vector2.Distance(start, curtouch) <= 10)
                    {
                        if (giaodien.Equals("menu"))
                        {
                            #region giao dien menu
                            if (btn_setting.isTouch(curtouch))
                            {
                                btn_setting.isTouched = false;
                                giaodien = "setting";
                            }
                            else if (btn_exit.isTouch(curtouch))
                            {
                                btn_exit.isTouched = false;
                                MessageBoxResult m = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "", MessageBoxButton.OKCancel);
                                if (m == MessageBoxResult.OK)
                                {
                                    // Application.Current.Terminate();
                                    Microsoft.Xna.Framework.Game game = new Microsoft.Xna.Framework.Game();
                                    game.Exit();
                                }
                            }
                            else if (btn_share.isTouch(curtouch))
                            {
                                btn_share.isTouched = false;
                                ShareLinkTask shareLinkTask = new ShareLinkTask();

                                shareLinkTask.Title = "";
                                shareLinkTask.LinkUri = new Uri("http://www.windowsphone.com/en-us/store/app/lo%E1%BA%A1n-12-s%E1%BB%A9-qu%C3%A2n/1671d059-0330-43d3-9031-d48bbd4e9427", UriKind.Absolute);
                                shareLinkTask.Message = "Tôi đang chơi game, có bạn nào muốn chơi cùng không ?";

                                shareLinkTask.Show();
                            }
                            else if (btn_info.isTouch(curtouch))
                            {
                                btn_info.isTouched = false;
                                MessageBox.Show("chuc nang dang phat trien");
                            }
                            else if (btn_help.isTouch(curtouch))
                            {
                                btn_help.isTouched = false;
                                MessageBox.Show("chuc nang dang phat trien");
                            }
                            else if (btn_rate.isTouch(curtouch))
                            {
                                btn_rate.isTouched = false;
                                MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                                marketplaceReviewTask.Show();
                            }
                            else
                            {
                                NavigationService.Navigate(new Uri("/MainMap.xaml", UriKind.Relative));
                            }
                            #endregion
                        }
                        else if (giaodien.Equals("setting"))
                        {
                            #region giao dien setting
                            if (btn_ok.isTouch(curtouch))
                            {
                                btn_ok.isTouched = false;
                                audiosetting.SaveSetting(isMusic, isSound);
                                giaodien = "menu";
                            }
                            else TouchSetting(curtouch);
                            #endregion
                        }
                    }
                    #endregion
                }
            }
        }
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(bg, Vector2.Zero, new Rectangle(0, 0, 481, 321), Color.White, 0f, Vector2.Zero, 5 / 3f, SpriteEffects.None, 0);
            spriteBatch.Draw(logo, new Vector2(5, 5), new Rectangle(0, 0, 228, 146), Color.White, 0f, Vector2.Zero, 1.6f, SpriteEffects.None, 0);
            btn_setting.Draw(spriteBatch);
            btn_exit.Draw(spriteBatch);
            btn_share.Draw(spriteBatch);
            btn_info.Draw(spriteBatch);
            btn_help.Draw(spriteBatch);
            btn_rate.Draw(spriteBatch);
            spriteBatch.Draw(text_continue, new Vector2(230, 380), new Rectangle(0, 0, 339, 20), Color.White, 0f, Vector2.Zero, 1.2f, SpriteEffects.None, 0);
            countAmination++;
            if (countAmination >= 40) countAmination = 0;
            if (countAmination < 28)
            {
                amination_skill = contentManager.Load<Texture2D>("Images/Menu/Effect/ef" + (countAmination / 4 + 1).ToString());
                spriteBatch.Draw(amination_skill, new Vector2(28, 45), new Rectangle(0, 0, 70, 68), Color.White, 0f, Vector2.Zero, 1.5f, SpriteEffects.None, 0);
            }
            if (giaodien.Equals("setting"))
            {
                spriteBatch.Draw(bgsetting, new Vector2(194, 124), Color.White);
                spriteBatch.Draw(img_music, new Vector2(485, 192), Color.White);
                spriteBatch.Draw(img_sound, new Vector2(485, 245), Color.White);
                btn_ok.Draw(spriteBatch);
            }
            spriteBatch.End();
        }

        private void TouchSetting(Vector2 touch)
        {
            if (touch.X >= 470 && touch.X <= 530)
            {
                if (touch.Y >= 240 && touch.Y <= 280)
                {
                    isSound = !isSound;
                    img_sound = contentManager.Load<Texture2D>("Images/Setting/" + (isSound ? "v" : "x"));
                }
                if (touch.Y >= 185 && touch.Y <= 225)
                {
                    isMusic = !isMusic;
                    img_music = contentManager.Load<Texture2D>("Images/Setting/" + (isMusic ? "v" : "x"));
                }
            }
        }
    }
}